package com.guardchina.framework.web.impl;

import com.guardchina.framework.util.exception.login.TokenExpException;
import com.guardchina.framework.web.api.TokenValidator;
import com.guardchina.framework.web.model.UserFeature;
import com.guardchina.framework.web.properties.LoginProperties;
import lombok.extern.slf4j.Slf4j;

/**
 * 描述：token过期校验
 *
 * @author lwb
 * @date 2019/3/19 15:23
 */
@Slf4j
public class TokenExpValidator implements TokenValidator {

    private LoginProperties properties;

    public TokenExpValidator(LoginProperties properties){
        this.properties = properties;
    }

    @Override
    public void verify(UserFeature userFeature){
        long currentTime = System.currentTimeMillis();

        //创建时间加上有效时间 小于当前时间，则过期
        if(userFeature.getCreateTime() + properties.getMaxAge() < currentTime){
            log.debug("登录日期：{}加上 最大过期时间：{}小于当前时间：{}登录过期", userFeature.getCreateTime(), properties.getMaxAge(), currentTime);
            throw TokenExpException.error();
        }
    }
}
